package cn.edu.zut.DAO;

import cn.edu.zut.model.Student;
import cn.edu.zut.model.Course;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository(value = "bdi1")
public class StudentDAOImpl extends AbstractStudentDAO {

    @Override
    public void insertStudent(Student student) {

        String INSERT_Student= " INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept,Sphones) VALUES(?,?,?,?,?,?) ";
        String INSERT_Course = " INSERT INTO Course(Cno,Cname,Cpno,TestCredit,RegularCredit,Csno) VALUES(?,?,?,?,?,?) ";

        Object[] args = { student.getSno(), student.getSname(), student.getSsex(),
                student.getSage(), student.getSdept(), student.getSphones() };
        jdbcTemplate.update(INSERT_Student,args);

        for(Course course : student.getCourses()){
            Object[] co = {course.getCno(),course.getCname(),course.getCpno(),course.getTestCredit(),course.getRegularCredit(),student.getSno()};
            jdbcTemplate.update(INSERT_Course,co);
        }

    }

    @Override
    public void insertAll(List<Student> studentList) {
        for(Student student:studentList){
            this.insertStudent(student);
        }
    }

    @Override
    public void modifyStudent(Student student) {
        String MODIFY_Student = " update Student set Sno=?, Sname=?, Ssex=?, Sage=?, Sdept=?,Sphones=? ";

        Object[] args = { student.getSno(), student.getSname(), student.getSsex(),
                student.getSage(), student.getSdept(), student.getSphones() };
        jdbcTemplate.update(MODIFY_Student,args);

    }

    @Override
    public void modifySumCredit(Student student) {
        String MODIFY_Course = " update Course set SumCredit=? where Csno=? ";
        for (Course course : student.getCourses()){
            Object[] co = {course.getSumCredit(),student.getSno()};
            jdbcTemplate.update(MODIFY_Course,co);
        }
    }

    @Override
    public Student queryOneStudent(Student student) {
        String QUERY_ONE_Student = " SELECT * FROM Student WHERE Sno = ? ";
        String QUERY_ALL_Course = " SELECT * FROM Course WHERE Csno = ? ";
        Object[] args = {student.getSno()};
        RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
        students = jdbcTemplate.query(QUERY_ONE_Student,args,rowMapper);
        student = students.get(0);
        RowMapper<Course> rowMapper1 = new BeanPropertyRowMapper<>(Course.class);
        student.setCourses(jdbcTemplate.query(QUERY_ALL_Course, args, rowMapper1));
        return student;
    }



    /*@Override
    public List<Course> queryOneStudentAllCourse(Student student) {
        String QUERY_ALL_Course = " SELECT * FROM Course where Course.Csno = ? ";
        Object[] co = {student.getSno()};
        RowMapper<Course> rowMapper = new BeanPropertyRowMapper<>(Course.class);
        courses = jdbcTemplate.query(QUERY_ALL_Course,co,rowMapper);
        return courses;
    }*/

    @Override
    public List<Student> queryAllStudent() {
        String QUERY_ALL_Student = " SELECT * FROM Student ";
        String QUERY_ALL_Course = " SELECT * FROM Course where Csno = ? ";

        RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
        students = jdbcTemplate.query(QUERY_ALL_Student,rowMapper);
        for(Student student : students){
            Object[] args = {student.getSno()};
            RowMapper<Course> rowMapper1 = new BeanPropertyRowMapper<>(Course.class);
            student.setCourses(jdbcTemplate.query(QUERY_ALL_Course, args, rowMapper1));
        }
        return students;
    }



   /* @Override
    public List<Student> queryAllStudent() {
        String QUERY_ALL_Student = " SELECT * FROM Student ";
        RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);//注意一下，解释一下
        students = jdbcTemplate.query(QUERY_ALL_Student,rowMapper);
        for(int i=0;i<students.size();i++) {
            courses = queryOneStudentAllCourse(students.get(i));
            for(int j=0;j<courses.size();j++) {
                Course course = courses.get(j);
                System.out.println(courses.size());
                System.out.println(course);
                System.out.println(students.size());
                System.out.println(students.get(i).getCourses());
                students.get(i).getCourses().add(course);
            }
        }
        return students;
    }*/

   /* public List<Student> queryAllStudent() {
        String QUERY_ALL_Student = " SELECT * FROM Student ";
        RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);//注意一下，解释一下
        students = jdbcTemplate.query(QUERY_ALL_Student,rowMapper);
        for(int i=0;i<students.size();i++) {
            courses = queryOneStudentAllCourse(students.get(i));
            for(int j=0;j<courses.size();j++) {
                Course course = courses.get(j);
                System.out.println(courses.size());
                System.out.println(course);
                System.out.println(students.size());
                System.out.println(students.get(i).getCourses());
                students.get(i).getCourses().add(course);
            }
        }
        return students;
    }*/

}
